package com.pactera.miyuangroup.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.pactera.miyuangroup.db.entity.LoginUser;
import com.pactera.miyuangroup.service.ISmsService;
import com.pactera.miyuangroup.service.LoginUserService;
import com.pactera.miyuangroup.service.impl.SmsServiceImp;
import com.pactera.miyuangroup.vo.SmsResult;
import com.pactera.miyuangroup.vo.request.app.AppLoginPara;
import com.pactera.miyuangroup.vo.request.web.LoginPara;
import com.pactera.miyuangroup.vo.request.Sms;
import com.pactera.miyuangroup.vo.response.AccessToken;
import com.pactera.miyuangroup.vo.response.ResultModel;
import com.pactera.miyuangroup.vo.response.ResultStatusCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * 登录token
 *
 * @author Wangwu
 * @create 2018-03-30 下午4:10
 **/
@Api(description = "登录授权接口")
@RestController
@RequestMapping("oauth")
public class JsonWebToken {

    @Autowired
    private LoginUserService loginUserService;
    @Autowired
    private ISmsService smsService;

    /**
     * web登录
     *
     * @param loginPara
     * @return
     */
    @ApiOperation(value = "用户名密码登录", notes = "web登录")
    @RequestMapping(value = "/web/token", method = RequestMethod.POST)
    public ResultModel<AccessToken> getWebAccessToken(@RequestBody LoginPara loginPara) {
        AccessToken accessToken = loginUserService.loginWeb(loginPara);
        return ResultModel.ok(accessToken);
    }

    /**
     * APP登录
     *
     * @param loginPara
     * @return
     */
    @ApiOperation(value = "手机号码短信验证码登录", notes = "APP登录")
    @RequestMapping(value = "/app/token", method = RequestMethod.POST)
    public ResultModel<AccessToken> getAppAccessToken(@RequestBody AppLoginPara loginPara) {
        return loginUserService.loginApp(loginPara);
    }

    /**
     * 短信发送
     *
     * @param sms
     * @return
     */
    @ApiOperation(value = "发送短信验证码", notes = "短信验证码")
    @RequestMapping(value = "/sms", method = RequestMethod.POST)
    public ResultModel sendSms(@RequestBody Sms sms) {

        EntityWrapper<LoginUser> ew = new EntityWrapper<LoginUser>();
        ew.eq("user_name", sms.getPhone());
        ew.eq("app", Boolean.TRUE);//是APP用户
        LoginUser loginUser = loginUserService.selectOne(ew);
        if (null == loginUser) {
            return ResultModel.fail(ResultStatusCode.USER_PHONE_NOT_EXIST);
        }
        SmsResult res = smsService.loginCode(sms.getPhone());
        if (!res.isSuccess()) {
            return ResultModel.fail(res.getMsg());
        }
        return ResultModel.ok();
    }

}
